package net.oschina.git.fsppp.model;

import net.oschina.git.fsppp.utils.P3Utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 磁盘类
 */
public class P3Disk implements Serializable {
    /**
     * name: String 磁盘名
     * e.g. C
     */
    private String name;
    /**
     * blocks: List 磁块
     * default size=128
     */
    private List<P3Block> blocks;

    /**
     * int: current 当前磁盘块指针
     */
    private int current = 0;

    public P3Disk() {
        this.blocks = new ArrayList<P3Block>(128);
    }

    /**
     * 根据Fat初始化磁盘，fat占两个磁盘块
     * @param fat
     */
    public void initDisk(P3FAT fat) {
        P3Block block_fat1 = new P3Block(false, 0);
        block_fat1.setDisk(this);
        P3Utils.copyByte(fat.getContent(), block_fat1.getContent(), 0, P3Utils.MAX_BLOCK_SIZE,0);
        P3Block block_fat2 = new P3Block(false, 1);
        block_fat1.setDisk(this);
        P3Utils.copyByte(fat.getContent(), block_fat2.getContent(), 64, P3Utils.MAX_BLOCK_SIZE*2,0);
        this.blocks.add(block_fat1);//FAT1
        this.blocks.add(block_fat2);//FAT2
        //init 126 block
        for (int i =2;i<128;i++) {
            P3Block block = new P3Block(false, i);
            block_fat1.setDisk(this);
            block.initBlock();
            this.blocks.add(block);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<P3Block> getBlocks() {
        return blocks;
    }

    public void setBlocks(List<P3Block> blocks) {
        this.blocks = blocks;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    @Override
    public int hashCode() {
        int hash = 17;

        if (this.name != null) {
            hash = hash * 31 + this.name.hashCode();
        }

        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || !this.getClass().equals(obj.getClass())) {
            return false;
        }

        P3Disk p = (P3Disk) obj;

        return (
                this.name != null &&
                this.name.equals(p.getName())
                );
    }

    @Override
    public String toString() {
        return name + "：\\";
    }
}
